package com.ayu.envdemo.schedule;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.ayu.envdemo.entity.DeviceMonitorMongodb;
import com.ayu.envdemo.sender.IMqttSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * @program: anlangaosu
 * @description: 定时任务
 * @author: youjianzhao
 * @create: 2022-06-28 09:48
 **/
@Configuration
@Slf4j
public class ScheduleTask {


    @Value("${auto.insert.data:}")
    private boolean flag;

    @Value("${iot.access.token:}")
    private String accessToken;

    @Value("${iot.host:}")
    private String host;

    @Value("${iot.mqtt.topic:}")
    private String mqttTopic;

    private String listStr = "[{\"alarmType\":\"0\",\"deviceAddress\":\"8\",\"deviceSn\":\"19_bxj_cqzx_8\",\"directionInfo\":\"cqzx\",\"highAlarm\":\"800\",\"id\":\"19_bxj_cqzx_8\",\"lowAlarm\":\"500\",\"section\":\"19\",\"sensorName\":\"SiO2\",\"sensorValue\":\"273\",\"symbols\":\"SiO2\",\"tunnel\":\"bxj\",\"unitValue\":\"273 ug/m3\",\"updateTime\":1655364173000},{\"alarmType\":\"0\",\"deviceAddress\":\"4\",\"deviceSn\":\"19_bxj_cqzx_4\",\"directionInfo\":\"cqzx\",\"highAlarm\":\"0.5\",\"id\":\"19_bxj_cqzx_4\",\"lowAlarm\":\"0.2\",\"section\":\"19\",\"sensorName\":\"二氧化碳\",\"sensorValue\":\"0.09\",\"symbols\":\"CO2\",\"tunnel\":\"bxj\",\"unitValue\":\"0.09 %vol\",\"updateTime\":1655364173000},{\"alarmType\":\"0\",\"deviceAddress\":\"2\",\"deviceSn\":\"19_bxj_cqzx_2\",\"directionInfo\":\"cqzx\",\"highAlarm\":\"50\",\"id\":\"19_bxj_cqzx_2\",\"lowAlarm\":\"25\",\"section\":\"19\",\"sensorName\":\"甲烷\",\"sensorValue\":\"0\",\"symbols\":\"CH4\",\"tunnel\":\"bxj\",\"unitValue\":\"0 %LEL\",\"updateTime\":1655364173000},{\"alarmType\":\"0\",\"deviceAddress\":\"5\",\"deviceSn\":\"19_bxj_cqzx_5\",\"directionInfo\":\"cqzx\",\"highAlarm\":\"50\",\"id\":\"19_bxj_cqzx_5\",\"lowAlarm\":\"25\",\"section\":\"19\",\"sensorName\":\"一氧化氮\",\"sensorValue\":\"3\",\"symbols\":\"NO\",\"tunnel\":\"bxj\",\"unitValue\":\"3 ppm\",\"updateTime\":1655364173000},{\"alarmType\":\"0\",\"deviceAddress\":\"7\",\"deviceSn\":\"19_bxj_cqzx_7\",\"directionInfo\":\"cqzx\",\"highAlarm\":\"10\",\"id\":\"19_bxj_cqzx_7\",\"lowAlarm\":\"5\",\"section\":\"19\",\"sensorName\":\"二氧化硫\",\"sensorValue\":\"0\",\"symbols\":\"SO2\",\"tunnel\":\"bxj\",\"unitValue\":\"0 ppm\",\"updateTime\":1655364173000},{\"alarmType\":\"0\",\"deviceAddress\":\"6\",\"deviceSn\":\"19_bxj_cqzx_6\",\"directionInfo\":\"cqzx\",\"highAlarm\":\"10\",\"id\":\"19_bxj_cqzx_6\",\"lowAlarm\":\"5\",\"section\":\"19\",\"sensorName\":\"二氧化氮\",\"sensorValue\":\"0.2\",\"symbols\":\"NO2\",\"tunnel\":\"bxj\",\"unitValue\":\"0.2 ppm\",\"updateTime\":1655364173000},{\"alarmType\":\"0\",\"deviceAddress\":\"3\",\"deviceSn\":\"19_bxj_cqzx_3\",\"directionInfo\":\"cqzx\",\"highAlarm\":\"150\",\"id\":\"19_bxj_cqzx_3\",\"lowAlarm\":\"50\",\"section\":\"19\",\"sensorName\":\"一氧化碳\",\"sensorValue\":\"6\",\"symbols\":\"CO\",\"tunnel\":\"bxj\",\"unitValue\":\"6 ppm\",\"updateTime\":1655364173000},{\"alarmType\":\"0\",\"deviceAddress\":\"1\",\"deviceSn\":\"19_bxj_cqzx_1\",\"directionInfo\":\"cqzx\",\"highAlarm\":\"23\",\"id\":\"19_bxj_cqzx_1\",\"lowAlarm\":\"18\",\"section\":\"19\",\"sensorName\":\"氧气\",\"sensorValue\":\"20.5\",\"symbols\":\"O2\",\"tunnel\":\"bxj\",\"unitValue\":\"20.5 %vol\",\"updateTime\":1655364173000},{\"alarmType\":\"0\",\"deviceAddress\":\"9\",\"deviceSn\":\"19_bxj_cqzx_9\",\"directionInfo\":\"cqzz\",\"highAlarm\":\"45\",\"id\":\"19_bxj_cqzx_9\",\"lowAlarm\":\"5\",\"section\":\"19\",\"sensorName\":\"温度\",\"sensorValue\":\"40\",\"symbols\":\"temperature\",\"tunnel\":\"bxj\",\"unitValue\":\"40 °C\"},{\"alarmType\":\"0\",\"deviceAddress\":\"10\",\"deviceSn\":\"19_bxj_cqzx_10\",\"directionInfo\":\"cqzz\",\"highAlarm\":\"85\",\"id\":\"19_bxj_cqzx_10\",\"lowAlarm\":\"75\",\"section\":\"19\",\"sensorName\":\"湿度\",\"sensorValue\":\"80\",\"symbols\":\"%RH\",\"tunnel\":\"bxj\",\"unitValue\":\"80 %RH\"},{\"alarmType\":\"0\",\"deviceAddress\":\"11\",\"deviceSn\":\"19_bxj_cqzx_11\",\"directionInfo\":\"cqzz\",\"highAlarm\":\"35\",\"id\":\"19_bxj_cqzx_11\",\"lowAlarm\":\"10\",\"section\":\"19\",\"sensorName\":\"pm2.5\",\"sensorValue\":\"20\",\"symbols\":\"PM2.5\",\"tunnel\":\"bxj\",\"unitValue\":\"20 mg/m3\"},{\"alarmType\":\"0\",\"deviceAddress\":\"12\",\"deviceSn\":\"19_bxj_cqzx_12\",\"directionInfo\":\"cqzz\",\"highAlarm\":\"150\",\"id\":\"19_bxj_cqzx_12\",\"lowAlarm\":\"10\",\"section\":\"19\",\"sensorName\":\"pm10\",\"sensorValue\":\"20\",\"symbols\":\"pm10\",\"tunnel\":\"bxj\",\"unitValue\":\"20 mg/m3\"}]";

    private List<DeviceMonitorMongodb> list = JSONObject.parseObject(listStr, new TypeReference<List<DeviceMonitorMongodb>>() {
    });


    private final IMqttSender iMqttSender;

    @Autowired
    public ScheduleTask(IMqttSender iMqttSender) {
        this.iMqttSender = iMqttSender;
    }


    @Scheduled(cron = "0/5 * * * * ?")
    public void doTask() {
        if (flag) {
            //http数据推送
            String newData = getNewData();
            pushData(newData);

            //mqtt数据推送
            String mqttData = getNewData();
            mqttPushData(mqttData);
        }
    }


    public String getNewData() {

        int max = list.size();
        //生成随机数
        int r = (int) (Math.random() * max);
        return JSONObject.toJSONString(list.get(r));

    }


    public void pushData(String json) {
        String url = host + "/api/v1/" + accessToken + "/telemetry";

        log.info("推送数据到：" + url + "\n" + json);

        HttpUtil.post(url, json);
    }

    /**
     * 2023/2/16 mqtt推送
     * @param json json
     */
    public void mqttPushData(String json) {

        log.info("Mqtt 推送数据到：" + mqttTopic + "\n" + json);
        iMqttSender.sendToMqtt(mqttTopic, json);

    }



    // TODO: 2023/2/17 消息发送到mq或者直接存到mongodb

    // TODO: 2023/2/17 数据清洗


}


